package rtool

import (
	"encoding/base64"
	"errors"
	"fmt"
	"math/big"
)

// Base64Decode base64解码
func Base64Decode(content string) (decodeString []byte, err error) {
	decodeString, err = base64.StdEncoding.DecodeString(content)
	if err == nil {
		return
	}
	decodeString, err = base64.RawStdEncoding.DecodeString(content)
	if err == nil {
		return
	}
	decodeString, err = base64.URLEncoding.DecodeString(content)
	if err == nil {
		return
	}
	decodeString, err = base64.RawURLEncoding.DecodeString(content)
	return
}

// NumberToBase 转换进制
// number：2~36进制的数字
// base：传入的number是多少进制的数字
// outBase：输出结果为多少进制的
func NumberToBase(number string, base int, outBase int) (string, error) {
	val, bl := (&big.Int{}).SetString(number, base)
	if false == bl {
		return "", errors.New(fmt.Sprint("传入的", number, "不是", base, "进制数据。"))
	}
	return val.Text(outBase), nil
}
